﻿using AutoMapper;
using MediatR;
using Purchase.ApiS.AllCation.Command.Quality;
using Purchase.Domian.Model;
using Purchase.ErrorCode;
using Purchase.Infrastructrue.Implenment;

namespace Purchase.ApiS.AllCation.Hanlder.Qualitypost
{
    public class UpdQualityCommandHanlder : IRequestHandler<UpdQualityCommand, ApiResult<int>>
    {
        DatabaseContext db;
        ILogger<UpdQualityCommandHanlder> logger;
        IMapper mapper;

        public UpdQualityCommandHanlder(DatabaseContext db, ILogger<UpdQualityCommandHanlder> logger, IMapper mapper)
        {
            this.db = db;
            this.logger = logger;
            this.mapper = mapper;
        }
        /// <summary>
        /// 更新质检信息方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<ApiResult<int>> Handle(UpdQualityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var depqu = mapper.Map<Quality>(request);
                depqu.IsDeleted = false;
                var model = await db.SqlSugar.Updateable(depqu).ExecuteCommandAsync();
                if (model > 0)
                {
                    result.Data = model;
                    result.Msg = "更新质检信息成功";
                    result.Code = ApiEume.Success;
                }
                else
                {
                    result.Data = model;
                    result.Msg = "更新质检信息失败";
                    result.Code = ApiEume.Error;

                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "更新质检信息失败");

            }
            return result;
        }
    }
}
